package com.ym.medical.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ym.core.exception.BusinessException;
import com.ym.core.shiro.ShiroUtil;
import com.ym.core.utils.tkmybatis.BaseSVImpl;
import com.ym.medical.dao.MindDao;
import com.ym.medical.domain.entity.MindEntity;
import com.ym.medical.domain.entity.PatientDetailEntity;
import com.ym.medical.domain.param.MindParam;
import com.ym.medical.domain.param.MindQueryMobileParam;
import com.ym.medical.domain.param.MindUpdateParam;
import com.ym.medical.domain.param.TdRecordExampleParam;
import com.ym.medical.domain.vo.MindListMobileVO;
import com.ym.medical.domain.vo.MindVo;
import com.ym.medical.domain.vo.ToMindTdRecordVO;
import com.ym.medical.enums.CodeRuleEnum;
import com.ym.medical.enums.InquiryOrderPaymentStateEnum;
import com.ym.medical.enums.MindThankSourceEnum;
import com.ym.medical.enums.PaymentStateEnum;
import com.ym.medical.service.IMindSV;
import com.ym.medical.service.IPatientDetailSV;
import com.ym.medical.service.ISysCodeRuleSV;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
                                                                                                                    
/**
* 心意Service业务层处理
*
* @author ym
* @date 2020-09-02
*/
@Service
@Transactional(rollbackFor = Exception.class)
public class MindSVImpl extends BaseSVImpl<MindEntity,MindDao> implements IMindSV {

	@Autowired
	private IPatientDetailSV patientDetailSV;

	@Autowired
	private ISysCodeRuleSV generateCodeRuleSV;

	@Override
	public PageInfo<ToMindTdRecordVO> listMindRecord(TdRecordExampleParam tdRecordExampleVO, int pageNo, int limit) {
		PageHelper.startPage(pageNo,limit);
		return new PageInfo<ToMindTdRecordVO>(mapper.listMindRecord(tdRecordExampleVO));
	}

	@Override
	public List<MindListMobileVO> selectListMobile(MindQueryMobileParam param) {
		List<MindListMobileVO> mindModels = mapper.selectListMobile(param);
		for (MindListMobileVO model : mindModels) {
			//如果是问诊订单显示患者的姓名
			if (MindThankSourceEnum.ORDER.getKey().equals(model.getThankSource())) {
				PatientDetailEntity patientDetail = patientDetailSV.selectByOrderId(model.getRelatedId());
				if (patientDetail != null) {
					model.setUserRealName(patientDetail.getName());
				}
			}
		}
		return mindModels;
	}

	@Override
	public int countByRelatedId(Long id, String thankSource) {
		MindEntity mindModel = new MindEntity();
		mindModel.setPayType(PaymentStateEnum.HAVE_TO_PAY.getKey());
		mindModel.setRelatedId(id);
		mindModel.setThankSource(thankSource);
		return countByParams(mindModel);
	}

	@Override
	public MindListMobileVO selectByOrderId(Long orderId) {
		MindParam mindModel = new MindParam();
		mindModel.setPayState(InquiryOrderPaymentStateEnum.HAVE_TO_PAY.getKey());
		mindModel.setRelatedId(orderId);
		mindModel.setThankSource(MindThankSourceEnum.ORDER.getKey());
		List<MindListMobileVO> mindListMobileVOS = selectListMobile(
				MindQueryMobileParam.builder()
				.build());
		return (mindListMobileVOS.isEmpty()) ? null : mindListMobileVOS.get(0);
	}

	@Override
	public MindListMobileVO getMindModelByIoNum(String ioNum) {
		List<MindListMobileVO> minds = selectListMobile(
				MindQueryMobileParam
						.builder()
						.ioNum(ioNum)
						.build()
		);
		return (minds.isEmpty()) ? null : minds.get(0);
	}

	/**
	 * 患者答谢医生
	 * @param currId
	 * @param ioId
	 * @return
	 */
	@Override
	public MindVo selectMind(Long currId, Long ioId) {
		return mapper.selectMind(currId,ioId);
	}

	@Override
	public Long sendHeart(MindUpdateParam param) {
		if((MindThankSourceEnum.ORDER.getKey().equals(param.getThankSource()) ||
				MindThankSourceEnum.TOPIC.getKey().equals(param.getThankSource())) &&
				param.getRelatedId() == null){
			throw new BusinessException("对订单或话题送心意时,关联id不能为空");
		}
		param.setUserId(ShiroUtil.getLoginId(true));
		param.setPayState(PaymentStateEnum.TO_BE_PAID.getKey());

		MindEntity mindEntity = param.covert();
		mindEntity.setId(null);
		mindEntity.setCreateTime(new Date());
		mindEntity.setIoNum(generateCodeRuleSV.createCode(CodeRuleEnum.SXY_ORDER));
		insert(mindEntity);
		return mindEntity.getId();
	}
}
